var app = getApp();
Page(Object.assign({},{
    data:{
        curPage : '/zofui_sales/admin/login',
        inited : false,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        
        var id = wx.getStorageSync('adminshopid');
        if(id*1 > 0){
            wx.navigateTo({
                url : '/zofui_sales/admin/index'
            })
        }
        app.com.auth(function(user){
            app.com.http('admin','POST',{op:'loginstart'},0,false,'','',function(res){
                if (!res.data.errno && res.data) {
                    self.setData({
                        admin : res.data.data.admin,
                        params : res.data.data.params
                    },function(){
                        self.setData({
                            inited : true,
                        });
                    });
                    // 关闭下拉刷新
                    wx.stopPullDownRefresh();
                }
            });
			app.com.setBar(app,self,options.shopid);
        },self);
        wx.setNavigationBarTitle({
            title: '登录',
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '登录',
            path: '',
        }
    },
    onPullDownRefresh: function () {
        this.onLoad();
    },
    input( e ){
        var type = e.currentTarget.dataset.name;
        var value = e.detail.value;
        this.setData({
            [`${type}`] : value,
        });
    },
    login(){
        var self = this;
        var post = {
            op:'login',
            account:self.data.account,
            pass:self.data.pass
        }
        if( !post.account ) {
            app.com.toast('请填写账户');return false;
        }
        if( !post.pass ) {
            app.com.toast('请填写密码');return false;
        }       

        app.com.http('admin','POST',post,0,true,'','',function(res){
            if (res.data && !res.data.errno) {
                app.com.toast('登录成功','success',function(){
                    var url = '/zofui_sales/admin/index';
                    if(app.globalData.adminurl){
                        url = '/'+app.globalData.adminurl;
                        app.globalData.adminurl = false;
                    }

                    wx.setStorageSync('adminshopid',res.data.data.shopid);

                    wx.navigateTo({
                        url : url
                    })
                });
            }else{
                app.com.toast( res.data.message );
            }
        });
    },
    fastlogin(){
        var self = this;
        if( self.data.admin.length <= 0 ) {
            app.com.toast('你的微信还没有绑定商户');return false;
        }
        if( self.data.admin.length == 1 ) {
            self.loginbyid( self.data.admin[0].shopid );return false;
        }
        self.setData({
            showlist : true,
        });

    },
    setLogin(shopid){
        wx.setStorageSync('adminshopid',shopid);
    },
    showuserbtn(){
        this.setData({
            showlist : !this.data.showlist,
        });
    },
    loginbyid(id){
        var self = this;
        var post = {
            op:'loginbyid',
            id:id,
        }
        app.com.http('admin','POST',post,0,true,'','',function(res){
            if (res.data && !res.data.errno) {
                app.com.toast('登录成功','success',function(){
                    var url = '/zofui_sales/admin/index';
                    if(app.globalData.adminurl){
                        url = '/'+app.globalData.adminurl;
                        app.globalData.adminurl = false;
                    }
                    wx.setStorageSync('adminshopid',id);

                    wx.navigateTo({
                        url : url
                    })
                });
            }else{
                app.com.toast( res.data.message );
            }
        });
    },
    loginthis(e){
        var id = e.currentTarget.dataset.id;
        this.loginbyid( id );
    },
    showJoin(e){
        var self = this;
        self.setData({
            Joinanimation : !self.data.Joinanimation,
        })
    },

    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    }, 
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },  
    callphone : function(e){
        app.com.callphone( e );
    },
    tel : function(e){
        app.com.callphone( e );
    },
    copy(e){
        wx.setClipboardData({
            data : e.currentTarget.dataset.copy
        })
    },

    
}))